﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TamGiac;
namespace GiaoDienTamGiac
{
    public partial class Form1 : Form
    {
        LopTamGiac tamGiac;
        bool ve;
        public Form1()
        {
            tamGiac = new LopTamGiac(0, 0, 1, 0, 2, 2);
            ve = false;
            InitializeComponent();
        }

        private void but_Tinh_Click(object sender, EventArgs e)
        {
            if (txt_ax.Text.Length < 1 || txt_ay.Text.Length < 1 ||
                txt_bx.Text.Length < 1 || txt_by.Text.Length < 1 ||
                txt_cx.Text.Length < 1 || txt_cy.Text.Length < 1)
            {
                MessageBox.Show("Chua nhap day du du lieu.  Vu long nhap day du du lieu.");
                return;
            }


            int ax = int.Parse(txt_ax.Text.Trim());
            int ay = int.Parse(txt_ay.Text.Trim());
            int bx = int.Parse(txt_bx.Text.Trim());
            int by = int.Parse(txt_by.Text.Trim());
            int cx = int.Parse(txt_cx.Text.Trim());
            int cy = int.Parse(txt_cy.Text.Trim());
            tamGiac = new LopTamGiac(ax, ay, bx, by, cx, cy);
            txt_KetQua.Text =TamGiac.LopTamGiac.LoaiTamGiac[tamGiac.XacDinhLoaiTamGiac()];

            ve = true;
            this.Refresh();
        }



        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private void VeTamGiac(Graphics g)
        {
            if (!tamGiac.KiemTraTamGiac())
                return;
            float x1, x2, y1, y2;
            x1 = tamGiac.A.X*1000%293 +350;
            y1 = tamGiac.A.Y * 1000 % 293 + 60;
            x2 = tamGiac.B.X * 1000 % 293 + 350;
            y2 = tamGiac.B.Y * 1000 % 293 + 60;

            g.DrawLine(Pens.Red, x1, y1, x2, y2);
            x2 = tamGiac.C.X * 1000 % 293 + 350;
            y2 = tamGiac.C.Y * 1000 % 293 + 60;
            g.DrawLine(Pens.Red, x1, y1, x2, y2);
            x1 = tamGiac.B.X * 1000 % 293 + 350;
            y1 = tamGiac.B.Y * 1000 % 293 + 60;
            g.DrawLine(Pens.Red, x1, y1, x2, y2);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;

            //Pen p = new Pen(Color.Blue);
            //p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            if(ve)
            VeTamGiac(g);
        }

        private void KhoaPhim(KeyPressEventArgs e, TextBox tb)
        {
            if (tb.Text.Length > 7)
            {
                e.Handled = true;
            }
            if (e.KeyChar == 8)
                e.Handled = false;

            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        private void txt_ax_KeyPress(object sender, KeyPressEventArgs e)
        {
            KhoaPhim(e, txt_ax);
        }

        private void txt_ay_KeyPress(object sender, KeyPressEventArgs e)
        {

            KhoaPhim(e, txt_ay);
        }

        private void txt_bx_KeyPress(object sender, KeyPressEventArgs e)
        {
            KhoaPhim(e, txt_bx);
        }

        private void txt_by_KeyPress(object sender, KeyPressEventArgs e)
        {
            KhoaPhim(e, txt_by);
        }

        private void txt_cx_KeyPress(object sender, KeyPressEventArgs e)
        {
            KhoaPhim(e, txt_cx);
        }

        private void txt_cy_KeyPress(object sender, KeyPressEventArgs e)
        {
            KhoaPhim(e, txt_cy);
        }

        private void but_Thoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
